package com.ml.arrecegor.climaplanetario.controller.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;

import com.ml.arrecegor.climaplanetario.manager.PlanetaManager;
import com.ml.arrecegor.climaplanetario.model.Pronostico;
import com.ml.arrecegor.climaplanetario.service.PronosticoService;

@Controller
public class PronosticoRestController {
	
	@Autowired 
	private PronosticoService pronosticoService;

	@Autowired
	private PlanetaManager planetaManager;
	
	@RequestMapping(value = "/clima", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)

	public ModelAndView climaRestService(@RequestParam(value = "dia", required=true) int dia) {
		Pronostico pronostico = pronosticoService.pronosticar(planetaManager.getPlanetas(), dia);
		Map<String, Object> respuesta = new HashMap<String, Object>();
		respuesta.put("dia", dia);
		respuesta.put("clima", pronostico.getEstado().getDescripcion());
		return new ModelAndView("jsonView", respuesta);
	}
	
	@RequestMapping(value = "/clima/extendido/", method = RequestMethod.GET)
	@ResponseStatus(HttpStatus.OK)

	public ModelAndView extendido10añosRestService() {
		List<Pronostico> pronosticos = new ArrayList<Pronostico>();
		for (int i = 1; i<3601; i++) {
			pronosticos.add(pronosticoService.pronosticar(planetaManager.getPlanetas(), i));
		}
		Map<String, Object> respuesta = new HashMap<String, Object>();
		respuesta.put("extendido", pronosticos);
		return new ModelAndView("jsonView", respuesta);
	}
	
}
